package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserCredit;
import cn.wolfcode.trip.base.mapper.CreditDetailMapper;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.mapper.UserCreditMapper;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.util.DateUtil;
import cn.wolfcode.trip.base.util.SignMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SignServiceImpl implements ISignService {

    @Autowired
    private CreditDetailMapper detailMapper;
    @Autowired
    private SignMapper signMapper;

    @Autowired
    private UserCreditMapper creditMapper;

    @Override
    public void save(Sign sign) {
        signMapper.insert(sign);
    }

    @Override
    public void delete(Long id) {
        signMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Sign> selectAll() {
        return signMapper.selectAll();
    }

    @Override
    public void update(Sign sign) {
        signMapper.updateByPrimaryKey(sign);
    }

    @Override
    public Sign get(Long id) {
        return signMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据用户id返回用户的签到信息
     *
     * @param userId
     * @return
     */
    @Override
    public SignMessage sign(Long userId,Long creditCatalogId) {
        // 根据积分分类id和细节名称查询出积分的值
        int grade = detailMapper.selectGradeByCatalogIdAndName(creditCatalogId,"签到");
        SignMessage result = new SignMessage();
        // 判断用户是否第一次签到,如果是,向签到表中插入一条数据,连续签到次数设置为1,总签到次数加一,返回签到信息对象
        Sign sign = signMapper.getSignInfoByUserId(userId);
        // 获取用户的积分信息
        UserCredit userCredit = creditMapper.selectByUserId(userId);
        // 用户第一次签到
        if (sign == null) {
            sign = new Sign();
            sign.setCount(1);
            sign.setContinueSign(1);
            // 关联用户信息
            User user = new User();
            user.setId(userId);
            sign.setUser(user);
            // 添加创建时间和更新时间
            sign.setCreateTime(new Date());
            sign.setUpdateTime(new Date());
            signMapper.insert(sign);
            // 用户首次获取积分
            if(userCredit == null){
                 userCredit = new UserCredit();
                 userCredit.setUser(user);
                 userCredit.setCurrentCredit(grade);
                 userCredit.setTotalCredit(grade);
                 userCredit.setCount(1);
                 userCredit.setUpdateTime(new Date());
                creditMapper.insert(userCredit);
            } else{
                // 限制用户次数
                if(DateUtil.getDateSubstract(new Date(),userCredit.getUpdateTime()) >= 1){
                    userCredit.setCount(0);
                }
                if (userCredit.getCount() >= 5) {
                    result.setSuccess(true);
                    result.mark("签到成功,积分获取次数达到上限,此次签到没有获得积分");
                    return result;
                }
                userCredit.setUpdateTime(new Date());
                userCredit.setCurrentCredit(userCredit.getCurrentCredit() + grade);
                userCredit.setTotalCredit(userCredit.getTotalCredit() + grade);
                userCredit.setCount(userCredit.getCount() + 1);
                creditMapper.updateByPrimaryKey(userCredit);
            }
            result.mark("签到成功,积分 +"+ grade +",继续加油!");
            result.setSuccess(true);
            return result;
        }
        // 获取当前签到时间
        Date updateTime = new Date();
        //  // 用户是连续签到,判断连续签到次数 + 1 是否等于 3 ,等于 3 有额外礼包,标记信息后再将连续签到次数置0,总签到次数加一
        if(DateUtil.getDateSubstract(updateTime,sign.getUpdateTime()) == 1){
            // 连续签到次数加1
            sign.setContinueSign(sign.getContinueSign() + 1);
            sign.setCount(sign.getCount() + 1);
            // 如果连续签到次数为三,那么有额外奖励
            if(sign.getContinueSign() % 3 == 0){
                // 更新签到时间
                sign.setUpdateTime(new Date());
                // 更新数据库信息
                signMapper.updateByPrimaryKey(sign);
                // 限制用户次数
                if(DateUtil.getDateSubstract(new Date(),userCredit.getUpdateTime()) >= 1){
                    userCredit.setCount(0);
                }
                if (userCredit.getCount() >= 5) {
                    result.setSuccess(true);
                    result.mark("签到成功,今日积分获取次数达到上限,此次签到没有获得积分");
                    return result;
                }

                userCredit.setUpdateTime(new Date());
                userCredit.setCount(userCredit.getCount() + 1);
                userCredit.setCurrentCredit(userCredit.getCurrentCredit() + (sign.getContinueSign() >= 5 ? 30 : sign.getContinueSign()*grade));
                userCredit.setTotalCredit(userCredit.getTotalCredit() + (sign.getContinueSign() >= 5 ? 30 : sign.getContinueSign()*grade));
                // 额外奖励
                userCredit.setCurrentCredit(userCredit.getCurrentCredit() + 50);
                userCredit.setTotalCredit(userCredit.getTotalCredit() + 50);
                creditMapper.updateByPrimaryKey(userCredit);
                result.setSuccess(true);
                result.setMsg("总共签到"+ sign.getCount() +"天,本次签到积分 +"+ (sign.getContinueSign() >= 5 ? 30 : sign.getContinueSign()*grade) +"签到满3天,获得额外奖励: 积分 + 50");
                return  result;
            }
           // 更新签到时间
             sign.setUpdateTime(new Date());
            // 更新数据库信息
            signMapper.updateByPrimaryKey(sign);
            // 限制用户次数
            if(DateUtil.getDateSubstract(new Date(),userCredit.getUpdateTime()) >= 1){
                userCredit.setCount(0);
            }
            if (userCredit.getCount() >= 5) {
                result.setSuccess(true);
                result.mark("签到成功,今日积分获取次数达到上限,此次签到没有获得积分");
                return result;
            }
            // 积分增加
            userCredit.setUpdateTime(new Date());
            userCredit.setCount(userCredit.getCount() + 1);
            userCredit.setCurrentCredit(userCredit.getCurrentCredit() + (sign.getContinueSign() >= 5 ? 30 : sign.getContinueSign()*grade));
            userCredit.setTotalCredit(userCredit.getTotalCredit() + (sign.getContinueSign() >= 5 ? 30 : sign.getContinueSign()*grade));
            creditMapper.updateByPrimaryKey(userCredit);
            result.setSuccess(true);
            result.setMsg("总共签到"+ sign.getCount() +"天,连续签到" + sign.getContinueSign() + "天,积分+"+ (sign.getContinueSign() >= 5 ? 30 : sign.getContinueSign()*grade)  +"真棒!");
            return  result;
        }

        if(DateUtil.getDateSubstract(updateTime,sign.getUpdateTime()) > 1){
            // 用户断签的情况
            // 新的签到更新时间
            sign.setUpdateTime(new Date());
            // 连续签到时间清0,加上此次签到数
            sign.setContinueSign(1);
            // 总签到次数加1
            sign.setCount(sign.getCount() + 1);
            // 更新数据库信息
            signMapper.updateByPrimaryKey(sign);
            // 次数限制
            if(DateUtil.getDateSubstract(new Date(),userCredit.getUpdateTime()) >= 1){
                userCredit.setCount(0);
            }
            // 积分增加
            if ( userCredit.getCount() >= 5) {
                result.setSuccess(true);
                result.mark("签到成功,今日积分获取次数达到上限,此次签到没有获得积分");
                return result;
            }
            userCredit.setUpdateTime(new Date());
            userCredit.setCount(userCredit.getCount() + 1);
            userCredit.setCurrentCredit(userCredit.getCurrentCredit() + grade);
            userCredit.setTotalCredit(userCredit.getTotalCredit() + grade);
            creditMapper.updateByPrimaryKey(userCredit);
            // 返回页面数据信息
            result.setMsg("签到成功,积分+" + grade + ",继续加油!");
            result.setSuccess(true);
            return result;
        }
        // 用户一天中点击两次签到
        if(DateUtil.getDateSubstract(updateTime,sign.getUpdateTime()) == 0){
            result.setSuccess(false);
            result.mark("您今天已经签过到了,请明天再来!");
        }

        // 添加创建时间和更新时间
        return result;
    }

    /**
     * 判断用户是否签到过,页面渲染
     * @param userId
     * @return
     */
    @Override
    public boolean isSign(Long userId) {
        Sign sign = signMapper.getSignInfoByUserId(userId);
        if(sign != null && DateUtil.getDateSubstract(new Date(),sign.getUpdateTime()) == 0){
            return true;
        }
        return false;
    }

}
